#import "FlutterPluginDemo2Plugin.h"
#import "FlutterNativeFactory.h"

#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MPVolumeView.h>

@implementation FlutterPluginDemo2Plugin


+ (instancetype)sharedInstance {
    static FlutterPluginDemo2Plugin *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}


#pragma mark - FlutterPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  FlutterMethodChannel* channel = [FlutterMethodChannel
      methodChannelWithName:@"flutter_plugin_demo2"
            binaryMessenger:[registrar messenger]];
  FlutterPluginDemo2Plugin* instance = [FlutterPluginDemo2Plugin sharedInstance];
  [registrar addMethodCallDelegate:instance channel:channel];
    
    FlutterEventChannel *eventChannel = [FlutterEventChannel eventChannelWithName:@"flutter_show_alert/event"  binaryMessenger:[registrar messenger]];
    [eventChannel setStreamHandler:instance];
    
    FlutterNativeFactory* factory =
          [[FlutterNativeFactory alloc] initWithMessenger:registrar.messenger];
      [registrar registerViewFactory:factory withId:@"native_view_show"];
}


- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  if ([@"getPlatformVersion" isEqualToString:call.method]) {
    result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
  }
  else if ([@"originalAllData" isEqualToString:call.method]) {
    result([@"iOS " stringByAppendingString:@"调取iOS原生成功"]);
  } 
  else if ([@"sendParamsToOriginal" isEqualToString:call.method]) {
      NSDictionary *params = call.arguments;
    NSLog(@"params = %@",params);
      int key = [params[@"key"] intValue];
      NSLog(@"key = %d",key);
    result([@"iOS端 - 计算翻10倍后为:" stringByAppendingString: [NSString stringWithFormat:@"%d",key * 10]]);
  }else if ([@"sendOriginalToHandle" isEqualToString:call.method]) {
      NSDictionary *params = call.arguments;
    NSLog(@"ios-params = %@",params);
    
      int value = [params[@"value"] intValue];
      if (self.eventSink) {
          NSDictionary *dic = @{
              @"key":@"getCount",
              @"value":[NSNumber numberWithInt:2 * value],
          };
          self.eventSink(dic);
      }    
  }
   else {
    result(FlutterMethodNotImplemented);
  }
}

#pragma mark - FlutterStreamHandler

- (FlutterError *)onListenWithArguments:(id)arguments eventSink:(FlutterEventSink)events {
    self.eventSink = events;
    NSLog(@"onListenWithArguments");
    
    MPVolumeView *volumeView = [[MPVolumeView alloc] init];
    UISlider *volumeViewSlider= nil;
    for (UIView *view in [volumeView subviews]){
        if ([view.class.description isEqualToString:@"MPVolumeSlider"]){
            volumeViewSlider = (UISlider*)view;
            break;
        }
    }

    
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        float currentVol = audioSession.outputVolume;
    NSDictionary *dic = @{
        @"key":@"changeVoice",
        @"value":[NSNumber numberWithFloat:currentVol],
    };
    self.eventSink(dic);
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(systemVolumChanged:)name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];
    
    
    return nil;
}

-(FlutterError *)onCancelWithArguments:(id)arguments {
    NSLog(@"onCancelWithArguments");
    self.eventSink = nil;
    return  nil;
}

-(void)systemVolumChanged:(NSNotification* )noti{
    //目前手机音量
    NSLog(@"systemVolumChanged----%@",noti.userInfo);
    float volume =  [noti.userInfo[@"AVSystemController_AudioVolumeNotificationParameter"] floatValue];
    NSDictionary *dic = @{
        @"key":@"changeVoice",
        @"value":[NSNumber numberWithFloat:volume],
    };
    self.eventSink(dic);
    
}

@end
